chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'sendMessage') {
    chrome.tabs.query({ url: 'https://www.doubao.com/chat/*' }, tabs => {
      if (tabs.length > 0) {
        chrome.scripting.executeScript({
          target: { tabId: tabs[0].id },
          func: msg => {
            window.postMessage({ type: 'DOUBAO_SEND_MESSAGE', message: msg }, '*')
          },
          args: [request.message],
        })
      }
    })
    sendResponse({ status: 'ok' })
  }
})

// 接收结果
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.type === 'DOUBAO_RESULT') {
    console.log('[DouBao] 收到结果:', request.result)
    // TODO: 可以转发给 popup.js
  }
})
